<template>
  <el-dialog :title="showTitle" :visible="showDialog" width="50%" @close="btnCancel">
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="1-50个字符"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择负责人" style="width: 100%" @focus="getEmployeeSimple">
          <el-option :label="item.username" :value="item.username" v-for="item in peoples" :key=item.id></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" placeholder="1-300个字符"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
    <el-button @click="btnCancel">取 消</el-button>
    <el-button type="primary" @click="btnOk">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script>
  import {addDepartments, getDepartments, updateDepartments, getDepartDetail} from "@/api/departments";
  import {getEmployeeSimple} from "@/api/employees";

  export default {
    name: "add-dept",
    props: {
      showDialog: {
        type: Boolean,
        default: false
      },
      treeNode: {
        type: Object,
        default: {}
      }
    },
    data() {
      const checkNameRepeat = async (rule, value, callback) => {

        const {depts} = await getDepartments()
        let isRepeat
        if (this.form.id) {
          isRepeat = depts.filter(item => item.pid === this.form.pid && item.id !== this.form.id).some(item => item.name === value)
        } else {
          isRepeat= depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
        }
        isRepeat ? callback(new Error(`同级部门下已经有${value}这个部门了`)) : callback()
      }
      const checkCodeRepeat = async (rule, value, callback) => {
        const {depts} = await getDepartments()
        let isRepeat = false
        if (this.form.id) {
          // 编辑
          isRepeat = depts.filter(item => item.id !== this.form.id).some(item => item.code === value)
        } else {
          isRepeat = depts.some(item => item.code === value) // 只要发现编码重复 就不行
        }
        isRepeat ? callback(new Error(`已经有${value}这个编码了`)) : callback()
      }
      return {
        form: {
          name: '', // 部门名称
          code: '', // 部门编码
          manager: '', // 部门管理者
          introduce: '' // 部门介绍
        },
        peoples: [],
        rules: {
          name: [
            {required: true, message: '请输入部门名称', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'},
            {trigger: 'blur', validator: checkNameRepeat}
          ],
          code: [
            {required: true, message: '请输入部门编码', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'},
            {trigger: 'blur', validator: checkCodeRepeat}
          ],
          manager: [
            {required: true, message: '请选择部门管理者', trigger: 'blur'}
          ],
          introduce: [
            {required: true, message: '请输入部门介绍', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 1 到 300 个字符', trigger: 'blur'}
          ],
        }
      }

    },

    methods: {
      btnOk() {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              await updateDepartments(this.form)
            } else {
              await addDepartments({...this.form, pid: this.treeNode.id})
            }
            this.$emit('update:showDialog', false)
            this.$emit('addDept')
          }

        })
      },
      btnCancel() {
        this.form = {
          name: '', // 部门名称
          code: '', // 部门编码
          manager: '', // 部门管理者
          introduce: '' // 部门介绍
        }
        this.$emit('update:showDialog', false)
        this.$refs.form.resetFields()
      },
      async getEmployeeSimple() {
        this.peoples = await getEmployeeSimple()
      },
      async getDepartDetail(id) {
        this.form = await getDepartDetail(id)
      }
    },
    computed: {
      showTitle() {
        return this.form.id ? '编辑部门' : '新增部门'
      }
    }

  }
</script>

<style scoped>

</style>
